﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Reflection;

using ERPStore.Web.Routing;

namespace ERPStore.Web.Html
{
	public static class ModelExtensions
	{
		public static MvcHtmlString RequiredStringFor<TModel, TPropery>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TPropery>> expression, string requiredString)
		{
			var metaData = System.Web.Mvc.ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
			if (!metaData.IsRequired)
			{
				return new MvcHtmlString(string.Empty);
			}
			return new MvcHtmlString(requiredString);
		}

		public static int MaxStringLengthFor<TModel, TPropery>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TPropery>> expression)
		{
			var metaData = System.Web.Mvc.ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
			if (metaData.AdditionalValues.ContainsKey("MaxStringLenght"))
			{
				return Convert.ToInt32(metaData.AdditionalValues["MaxStringLenght"]);
			}
			return 0;
		}

		public static int MinStringLengthFor<TModel, TPropery>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TPropery>> expression)
		{
			var metaData = System.Web.Mvc.ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
			if (metaData.AdditionalValues.ContainsKey("MinStringLenght"))
			{
				return Convert.ToInt32(metaData.AdditionalValues["MinStringLenght"]);
			}
			return 0;
		}

	}
}
